source('00_util_scripts/mod_bulk.R')
source('00_util_scripts/mod_bplot.R')

cd14ko_mf <- pluck_geo('GSE7237')

c7_alias <- cd14ko_mf |>
  fData() |>
  filter(str_detect(GENE, 'Trpc7')) |>
  rownames()

cd14ko_mf |>
  makeSummarizedExperimentFromExpressionSet() |>
  filter(.feature == c7_alias)

cd14ko_dc <- download_ncbi_counts('GSE161466')

cd14ko_dc_meta <- download_ncbi_meta('GSE161466')

cd14ko_dc_m2 <- cd14ko_dc |>
  pivot_longer(-1) |>
  calc_tpm(sample = name, abundance = value) |>
  filter(Symbol == 'TRPM2') |>
  left_join(cd14ko_dc_meta, join_by(name == geo_accession)) |>
  mutate(ko = str_extract(title, '[:alnum:]+ KO'),
         group = str_extract(title, '(?<=KO ).+(?= rep)'))

cd14ko_dc_m2 |>
  ggplot(aes(ko, log1p(tpm), fill = ko)) +
  stat_mean(geom = 'col') +
  geom_jitter(width = .1, height = 0) +
  facet_wrap(vars(group)) +
  labs(title = 'Human Mo-DC TRPM2 expression',
       subtitle = 'GSE161466') +
  rotate_x_text()

cd14ko_dc_tdb <- cd14ko_dc |>
  pivot_longer(-1) |>
  left_join(cd14ko_dc_meta, join_by(name == geo_accession)) |>
  mutate(ko = str_extract(title, '[:alnum:]+ KO'),
         group = str_extract(title, '(?<=KO ).+(?= rep)')) |>
  tidybulk(.sample = title, .transcript = Symbol, .abundance = value) |>
  quick_process_bulk(group = group, skip_scale = T)

cd14vctrl <- cd14ko_dc_tdb |>
  mutate(ko = replace_na(ko, 'Ctrl') |> make.names()) |>
  filter(str_detect(group, 'theta')) |>
  test_differential_abundance(~ 0 + ko, contrasts = 'koCD14.KO-koCtrl',
                              omit_contrast_in_colnames = T)

cd14vctrl |>
  pivot_transcript() |>
  filter(Symbol == 'TRPM2')

